<template>
  <div>
    <el-dialog
      title="添加优惠券"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="优惠券名" prop="name">
          <el-input v-model="form.name" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="选择商家">
          <el-select v-model="form.mid" multiple placeholder="请选择">
            <el-option
              v-for="item in mList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额">
          <el-input v-model="form.sum" style="width: 300px" type="number"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="form.count" style="width: 300px" type="number"></el-input>
        </el-form-item>
        <el-form-item label="天数">
          <el-input v-model="form.day" style="width: 300px" type="number"></el-input>
        </el-form-item>
        <el-form-item label="时间段">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio v-model="form.type" label="1">满减券</el-radio>
          <el-radio v-model="form.type" label="2">抵用券</el-radio>
        </el-form-item>
        <el-form-item label="是否发布">
          <el-switch
            v-model="form.status"
            active-value="1"
            inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="描述">
          <wang-enduit v-model="form.describ"></wang-enduit>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleAdd">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import {allList} from "@/api/merchant";
  import {couponAdd} from "@/api/coupon";
  import WangEnduit from '@/views/goods/components/wang-enduit'

  export default {
    name: "create",
    components: {
      WangEnduit
    },
    data() {
      return {
        dialogVisible: false,
        date: [],
        form: {
          name: '',
          mid: [],
          sum: '',
          count: '',
          day: '',
          type: '',
          status: '',
          describ: '',
          startTime: '',
          dueTime: ''
        },
        mList: [],
        rules:
          {
            // name: [{required: true, message: '请输入优惠券名称', trigger: 'blur'}]
          }
      }
    },
    methods: {
      show() {
        let form = {
          name: '',
          mid: [],
          sum: '',
          count: '',
          day: '',
          type: '',
          status: '',
          describ: '',
          startTime: '',
          dueTime: ''
        }
        this.form = form
        this.date = []
        this.dialogVisible = true
        allList().then(res => {
          this.mList = res.result
        })
      },
      handleAdd() {
        // console.log(this.date)
        this.form.startTime = this.date[0]
        this.form.dueTime = this.date[1]
        this.form.mid = this.form.mid.toString()
        couponAdd(this.form).then(res => {
          if (res.status === 200) {
            this.$message.success("添加成功")
            this.$emit('Ok')
            this.dialogVisible = false
          }
        })
      }
    },

  }
</script>

<style scoped>

</style>
